package com.ruoyi.eam.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.eam.complex.StokByAssetList;
import com.ruoyi.eam.constants.EamConstants;
import com.ruoyi.eam.doman.*;
import com.ruoyi.eam.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.eam.service.IEamStockService;

/**
 * 入库信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-20
 */
@Service
public class EamStockServiceImpl implements IEamStockService 
{
    @Autowired
    private EamStockMapper eamStockMapper;
    @Autowired
    private EamStockDMapper eamStockDMapper;
    @Autowired
    private EamAssetMapper eamAssetMapper;
    @Autowired
    private EamTypeMapper eamTypeMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EamLocationMapper eamLocationMapper;

    /**
     * 查询入库信息
     * 
     * @param stockId 入库信息主键
     * @return 入库信息
     */
    @Override
    public EamStock selectEamStockByStockId(Long stockId)
    {
        return eamStockMapper.selectEamStockByStockId(stockId);
    }

    /**
     * 查询入库信息列表
     * 
     * @param eamStock 入库信息
     * @return 入库信息
     */
    @Override
    public List<EamStock> selectEamStockList(EamStock eamStock)
    {
        List<EamStock> list = eamStockMapper.selectEamStockList(eamStock);
        for (EamStock stock : list) {
            EamAsset eamAsset = eamAssetMapper.selectEamAssetByAssetId(stock.getAssetId());
            if(eamAsset != null){
                EamType eamType = eamTypeMapper.selectEamTypeByTypeId(eamAsset.getTypeId());
                User user = userMapper.selectUserByUserId(stock.getHandlePerson());
                Dept dept = deptMapper.selectDeptByDeptId(user.getDeptId());
                EamLocation eamLocation = eamLocationMapper.selectEamLocationByLocationId(eamAsset.getLocationId());
                stock.setEamAsset(eamAsset);
                stock.setEamType(eamType);
                stock.setUser(user);
                stock.setDept(dept);
                stock.setEamLocation(eamLocation);
                if(stock.getAuditPerson() != null){
                    stock.setAuditPersonName(userMapper.selectUserByUserId(stock.getAuditPerson()).getNickName());
                    stock.setAuditDeptName(deptMapper.selectDeptByDeptId(userMapper.selectUserByUserId(stock.getAuditPerson()).getDeptId()).getDeptName());
                }
            }

        }
        return list;
    }

    /**
     * 新增入库信息
     * 
     * @param eamStock 入库信息
     * @return 结果
     */
    @Override
    public int insertEamStock(StokByAssetList stokByAssetList)
    {
        for (EamAsset eamAsset : stokByAssetList.getEamAssets()) {
            if(stokByAssetList.getStock().getStockId() != null){
                eamAsset.setLocationId(stokByAssetList.getStock().getEamAsset().getLocationId());
                eamAssetMapper.updateEamAsset(eamAsset);
            }
            EamStock stock = new EamStock();
            //业务处理
            stock.setCreateTime(DateUtils.getNowDate());
            stock.setStockTime(DateUtils.getNowDate());
            //入库编码规则
            //获取当前时间
            String nowDate = DateUtils.getDateYYYYMMDD();
            //获取最后一条入库编码
            String stockCodeLast = eamStockMapper.queryEamStockList().get(eamStockMapper.queryEamStockList().size() - 1).getStockcode();
            //截取流水线
            String serial =  stockCodeLast.substring(10,15);
            if(serial.equals("99999")){
                serial = "00001";
            }else{
                int num = Integer.parseInt(serial) + 1;
                serial = String.format("%05d",num);
            }
            //最终编码
            String stockCode = EamConstants.STOCK_CODE_PR + nowDate + serial;
            stock.setStockcode(stockCode);
            stock.setIsDel(1l);
            stock.setStockType(stokByAssetList.getStock().getStockType());
            stock.setCreator(stokByAssetList.getStock().getCreator());
            stock.setHandlePerson(stokByAssetList.getStock().getHandlePerson());
            stock.setAuditType(3L);
            stock.setAssetId(eamAsset.getAssetId());
            int num = eamStockMapper.insertEamStock(stock);
            if(num == 0){
                return 0;
            }
        }
        return 1;
    }

    /**
     * 修改入库信息
     * 
     * @param eamStock 入库信息
     * @return 结果
     */
    @Override
    public int updateEamStock(EamStock eamStock)
    {
        return eamStockMapper.updateEamStock(eamStock);
    }

    /**
     * 批量删除入库信息
     * 
     * @param stockIds 需要删除的入库信息主键
     * @return 结果
     */
    @Override
    public int deleteEamStockByStockIds(Long[] stockIds)
    {
        return eamStockMapper.deleteEamStockByStockIds(stockIds);
    }

    /**
     * 删除入库信息信息
     * 
     * @param stockId 入库信息主键
     * @return 结果
     */
    @Override
    public int deleteEamStockByStockId(Long stockId)
    {
        return eamStockMapper.deleteEamStockByStockId(stockId);
    }
}
